package org.mvnsearch.rsocket.responder;

import org.mvnsearch.account.Account;
import org.mvnsearch.account.AccountService;
import org.mvnsearch.exception.AbstractBizException;
import org.mvnsearch.protobuf.AccountInfoPoto.AccountInfo;
import org.mvnsearch.protobuf.AccountInfoPoto.AccountInfo.Builder;
import org.springframework.messaging.handler.annotation.MessageExceptionHandler;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.stereotype.Controller;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * account service implementation with RSocket exposed
 *
 * @author linux_china
 */
@Controller
@MessageMapping("org.mvnsearch.account.AccountService")
public class AccountServiceImpl implements AccountService {
    @Override
    @MessageMapping("findById")
    @MessageExceptionHandler(value= {AbstractBizException.class})
    public Mono<Account> findById(Integer id) {
        //return Mono.just(new Account(id, "nick:" + id));
    	AbstractBizException ex = new AbstractBizException("my customer error") {
    		
    	};
    	ex.setCode(500);
    	throw ex;
    }

    @Override
    @MessageMapping("findAll")
    public Flux<AccountInfo> findAll() {
        //return Flux.just(new Account(1, "Jackie"), new Account(2, "Tom"));
    	Account account = new Account(1, "Jackie");
    	Account account2 = new Account(2, "Tom");
    	Builder newBuilder = AccountInfo.newBuilder();
		newBuilder.setId(account.getId());
		newBuilder.setNick(account.getNick());
		AccountInfo acinfo = newBuilder.build();
		newBuilder = AccountInfo.newBuilder();
		newBuilder.setId(account2.getId());
		newBuilder.setNick(account2.getNick());
		AccountInfo acinfo2 = newBuilder.build();
		return Flux.just(acinfo, acinfo2);
    }
}
